﻿using Brc_PaaS.Api.Common;
using Brc_PaaS.Api.Filters;
using Brc_PaaS.Bll.System;
using Brc_PaaS.Common.Entity;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Common.Model.System;
using Brc_PaaS.Core.Redis;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;

namespace Brc_PaaS.Api.PassController
{
    /// <summary>
    /// PaaS平台自身业务实现调用
    /// </summary>
    [Route("PaaS/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    [ApiExplorerSettings(GroupName = "PaaSBasic")]
    public class PaaSSystemController : AdminBaseController
    {
        private readonly object Locker = new object();
        private ISystem _system;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="system"></param>
        public PaaSSystemController(IConfiguration configuration, ISystem system) : base(configuration)
        {
            _system = system;
        }

        /// <summary>
        /// 注册业务系统
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [LoginValidatorFilter]
        [HttpPost]
        public ActionResult<ResponseModel<SystemCallBackModel>> RegisterSystem(Bas_System model)
        {
            model.AccountId = Guid.NewGuid().ToString("N");
            model.CreateUserId = model.AccountId;
            var result = _system.RegisterSystem(model);
            return Ok(result);
        }

        /// <summary>
        /// 修改业务系统信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        
        [LoginValidatorFilter]
        [HttpPost]
        public ActionResult<BaseResponseModel> UpdateSystem(Bas_System model)
        {
            var result = _system.UpdateSystem(model);
            using (var redisManager = new RedisManager(RedisServerEntity))
            {
                redisManager.KeyDelete($"System:{model.Id}");
            }
            return Ok(result);
        }
    }
}